# What's for:
#   This is a script, to calculate and display 2+2.
#   In addition, I include some codes for my own practices.
#   For the homework 1 of 19704.
#
# On styles:
#   While I am trying to follow the google style guide, there is a strong inertia and urge to use my own style.
#   http://google-styleguide.googlecode.com/svn/trunk/Rguide.xml
#
# On practice codes:
#   The practice codes are drawn from examples and problems in the following books:
#   http://cran.r-project.org/doc/manuals/R-intro.pdf
#   http://cran.r-project.org/doc/contrib/Owen-TheRGuide.pdf
#
# Author:
#   Fan Tong, ftong@andrew.cmu.edu
#
# History:
#   Mar 19, 2014: Codes
#   Mar 24, 2014: Comments

# ----------------------
# ----------------------
# HW1 requirement

c <- 2+2
print(c)

# ----------------------
# ----------------------
# Getting help

help(rm)
help(disp)
help(print)

# ----------------------
# ----------------------
# Arithmetics

log2(c(1,2,3,4))
abs(2^3-3^2)
exp(exp(1))
help(pi)
2.3^8+log(7.5)-cos(pi/2^0.5)
log(7.5)

# ----------------------
# ----------------------

# Data frames and matrix manipulation

c(2,5,6,7)
c(2,5,6,7)*2
c(2,5,6,7)*c(2,5,6,7)
sum(c(2,5,6,7)*c(2,5,6,7))
t(c(2,5,6,7))
t(t(c(2,5,6,7)))
t(c(2,5,6,7)) %*% t(t(c(2,5,6,7)))
matrix_item <- c(1,2,3,4,5,6)
matrix <- matrix(matrix_item,3,2)

# Even though '=' works, google style guide suggests using '->'
A_item = c(1,2,3,2,2,1,6,4,4,7,2,5)
B_item = c(1,0,2,1,3,1,4,5,5,3,7,1,2,4,3,2)

# The 1st line should get an error!
A = matrix(A_item,3,4,false)
B = matrix(B_item,4,4,FALSE)

solve(B)
A%*%solve(B)
B%*%t(A)
help(sink)